package com.cn.sys.controller;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.cn.controller.AbstractController;
import com.cn.sys.entity.SysRoleEntity;
import com.cn.sys.service.SysRoleMenuService;
import com.cn.sys.service.SysRoleService;
import com.cn.utils.Constant;
import com.cn.utils.Query;
import com.cn.utils.FormSubmitResult;
import com.cn.utils.StringUtil;
import com.cn.utils.annotation.SysLog;
import com.cn.utils.validator.ValidatorUtils;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 角色管理
 * 
 */
@Controller
@RequestMapping(value ="/sys/role")
public class SysRoleController extends AbstractController {
	
	@Autowired
	private SysRoleService sysRoleService;
	

	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	private Logger logger=LoggerFactory.getLogger(getClass());
	/**
	 * 角色列表
	 */
	@RequestMapping(value = { "/list" })
	@ResponseBody
	public FormSubmitResult list(@RequestParam Map<String, Object> params){
		//查询列表数据
		PageInfo<SysRoleEntity> pageinfo=sysRoleService.findPage(params);
		return FormSubmitResult.SUCCESS().put("page", pageinfo);	
		
	}
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	public FormSubmitResult select(){
		Map<String, Object> map = new HashMap<>();
		
		List<SysRoleEntity> list = sysRoleService.findList(map);
		return FormSubmitResult.SUCCESS().put("list", list);
	}
	
	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	public FormSubmitResult info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.queryObject(roleId);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
//		role.setMenuIdList(menuIdList);
		
		return FormSubmitResult.SUCCESS().put("role", null);
	}
	
	/**
	 * 保存角色
	 * @throws Exception 
	 */
	@SysLog("保存角色")
	@RequestMapping("/save")
	@ResponseBody
	public FormSubmitResult save(@RequestBody SysRoleEntity role) throws Exception{
		
		ValidatorUtils.validateEntity(role);
		
		role.setCreateUserId(getUserId());
		sysRoleService.save(role);
		
		return FormSubmitResult.SUCCESS();
	}
	
	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@RequestMapping("/update")
	@ResponseBody
	public FormSubmitResult update(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		
//		role.setCreateUserId(getUserId());
		sysRoleService.update(role);
		
		return FormSubmitResult.SUCCESS();
	}
	
	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@ResponseBody
	public FormSubmitResult delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		
		return FormSubmitResult.SUCCESS();
	}
	
	@RequestMapping(value = "/roleOper",method = RequestMethod.GET)
    public ModelAndView sysRoleAddOper(String roleId) {
			SysRoleEntity entity=new SysRoleEntity();
			if(!StringUtils.isBlank(roleId)){
				Map<String, Object> params=new HashMap<String, Object>();
				params.put("roleId", roleId);
				entity=sysRoleService.get(params);
			}
			ModelAndView modelAndView=new ModelAndView("role/roleOper");
			modelAndView.addObject("role",entity);
            return modelAndView;
    }

}
